layui.config({
    base: '../../static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['form','index', 'useradmin', 'table'], function(){
    var table = layui.table,layer = layui.layer; //独立版的layer无需执行这一句
    var form=layui.form;

    table.render({
        elem: '#databaseList'
        ,height: 'full-100'
        ,limit:10
        ,async:false
        ,title:"数据备份表"
        ,page: true //开启分页
        ,url: '/database/listPage' //数据接口
        ,cellMinWidth: 80
        ,skin: 'row' //行边框风格
        ,even: true //开启隔行背景
        ,size: 'sm' //小尺寸的表格
        ,cols: [[ //表头
            {field: 'directory', title: '备份目录',  align: 'center'}
            ,{field: 'fileName', title: '文件名',  align: 'center'}
            ,{field: 'createtime', title: '备份时间',align: 'center'}
            ,{fixed: 'right',  title: '操作', width:300,  align:'center', toolbar: '#barDemo',align: 'center'}
        ]]
    });


    //执行备份
    $("#submit").click(function () {
        $.ajax({
            url:"/database/backup",
            type:"get",
            success:function (result) {
                if(result.code==0){
                    layer.msg(result.message, {icon: 1,time:2000,shade:0.2});
                    setInterval(function () {
                        window.location.reload();
                    },2000);
                }else {
                    layer.msg("数据备份失败", {icon: 2,time:2000,shade:0.2});
                }
            }
        });
        return false;
    })


    // 监听行工具事件
    table.on('tool(databaseList)', function(obj){
        var data = obj.data,layEvent = obj.event;
        if(layEvent === 'del'){
            layer.confirm('确定删除文件['+data.fileName+']吗？', {
                btn: ['是', '否']
            }, function(index, layero){
                $.ajax({
                    url:"/database/delete",
                    type:"get",
                    data:{id:data.id},
                    success:function (result) {
                        if(result.code==0){
                            layer.msg('删除成功！', {icon: 1,time:2000,shade:0.2});
                            setInterval(function () {
                                window.location.reload();
                            },500);
                        }else {
                            layer.alert(result.message, {icon: 2,time:2000,shade:0.2});
                        }

                    }
                });
            });
        }else if(layEvent === 'recover'){   //数据还原
            $.ajax({
                url:"/database/rollback",
                type:"get",
                data:{fileName:data.directory},
                success:function (result) {
                    if(result.code==0){
                        layer.msg(result.message, {icon: 1,time:2000,shade:0.2});
                        setInterval(function () {
                            window.location.reload();
                        },2000);
                    }else {
                        layer.msg(result.message, {icon: 2,time:2000,shade:0.2});
                    }
                }
            });
            return false;
        }
    });



});